package com.lsh.oms.core.proxy.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.model.http.HttpMessage;
import com.lsh.oms.core.utils.HttpUtils;
import org.apache.http.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * Project Name: lsh-oms
 * @author panxudong
 * 北京链商电子商务有限公司
 * Desc: 类功能描述
 * Package Name: com.lsh.oms.core.proxy.service
 * Time: 2017-08-28 下午2:29
 */
public abstract class BaseProxy {

    private static final Logger logger = LoggerFactory.getLogger(BaseProxy.class);

    private static final String SUCCESS = "0";
    private static final String CONTENT_KEY = "content";
    private static final String RET_KEY = "ret";

    public static boolean isSuccess(String responseMessage) {
        String ret = getRet(responseMessage);
        if (SUCCESS.equals(ret)) {
            return true;
        } else {
            return false;
        }
    }

    public static String getContent(String responseMessage) {

        logger.info("tms responseMessage is {} ",responseMessage);
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(responseMessage);
        }catch (Exception e){
            return null;
        }

        return jsonObject.getString(CONTENT_KEY);
    }

    public static String getRet(String responseMessage) {
        logger.info("getRet tms responseMessage is {} ",responseMessage);

        JSONObject jsonObject = JSONObject.parseObject(responseMessage);
        return jsonObject.getString(RET_KEY);
    }

    protected HttpMessage sendPostRequest(String message, Header[] headers, String url, Map<String, Object> params) throws IOException {
        HttpMessage httpMessage;
        try {
            logger.info(message + "[start][Request url:" + url + ", params:" + JSON.toJSONString(params) + "]");
            httpMessage = HttpUtils.sendPostRequest(url, params, headers);
            logger.info(message + "[end][Response result:" + JSON.toJSONString(httpMessage) + "]");
            return httpMessage;
        } catch (IOException e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
            logger.error(e.getMessage(), e);
            throw e;
        } catch (Exception e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    protected HttpMessage sendGetRequest(String message, Header[] headers, String url) throws IOException {
        HttpMessage httpMessage;
        try {
            logger.info(message + "[start][Request url:" + url + "]");
            httpMessage = HttpUtils.sendGetRequest(url, headers);
            logger.info(message + "[end][Response result:" + JSON.toJSONString(httpMessage) + "]");
            return httpMessage;
        } catch (IOException e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
            logger.error(e.getMessage(), e);
            throw e;
        } catch (Exception e) {
            logger.error(message + "[error][Error message:" + e.getMessage() + "]");
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

}
